//
//  KZExperienceEventCell.m
//  xiaorizi
//
//  Created by kun on 15/10/28.
//  Copyright © 2015年 origin. All rights reserved.
//

#import "KZExperienceEventCell.h"

#define  SCREEN_W  [UIScreen mainScreen].bounds.size.width

@interface KZExperienceEventCell ()<UIWebViewDelegate,UIActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@property (weak,nonatomic)UILabel *titleLabel;

@property (weak,nonatomic)UILabel *subTitleLabel;
@property (weak, nonatomic) IBOutlet UIView *phoneView;
@property (weak, nonatomic) IBOutlet UIView *MapView;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@end


@implementation KZExperienceEventCell

- (void)awakeFromNib {
    // Initialization code
   
    
    UILabel *titlelabel = [[UILabel alloc]init];
    titlelabel.frame = CGRectMake(8, -80, SCREEN_W, 40);
    [self.webView.scrollView addSubview:titlelabel];
    _titleLabel =titlelabel;
     titlelabel.backgroundColor = [UIColor clearColor];
    
    UILabel *subTitlelabel = [[UILabel alloc]init];
    subTitlelabel.frame = CGRectMake(8, -40, SCREEN_W, 40);
    [self.webView.scrollView addSubview:subTitlelabel];
    [subTitlelabel setFont:[UIFont systemFontOfSize:13]];
    [subTitlelabel setTextColor:[UIColor grayColor]];
    _subTitleLabel = subTitlelabel;
    subTitlelabel.backgroundColor = [UIColor clearColor];
    
    //设置webView
    self.webView.userInteractionEnabled = NO;
    self.webView.scrollView.bounces = NO;
    self.webView.scrollView.showsVerticalScrollIndicator = NO;
    self.webView.scalesPageToFit = NO;
    self.webView.scrollView.contentInset = UIEdgeInsetsMake(80, 0, 0, 0);
    
    //添加地图view用以 显示日期数据添加手势
   // [self createBottomView];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickedPhoneView:)];
    [_phoneView addGestureRecognizer:tap];
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickedMapView:)];
    [_MapView addGestureRecognizer:tap1];
    
    //设置代理
    self.webView.delegate = self;
    
}


//实现弹窗
-(void)clickedPhoneView:(UITapGestureRecognizer *)tap
{
    UIActionSheet *sheetView = [[UIActionSheet alloc]initWithTitle:@"11111111" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil, nil];
     [sheetView showInView:self];
}



//实现点击回调 地图显示
-(void)clickedMapView:(UITapGestureRecognizer *)tap
{
    
    
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setModel:(ExperienceEventsModel *)model
{
    _model = model;
    
    _titleLabel.text = model.title;
    _subTitleLabel.text = model.tag;
    
    
    _dateLabel.text = _model.date;
    _phoneLabel.text = model.telephone;
    _addressLabel.text = model.address;
    
    //加载web数据
   [self.webView loadHTMLString:model.mobileURL baseURL:[NSURL URLWithString:model.shareURL]];
}



#pragma  获取webView里面内容的大小

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    double width = webView.frame.size.width - 10;
    NSString *js = [NSString stringWithFormat:@"var script = document.createElement('script');"
                    "script.type = 'text/javascript';"
                    "script.text = \"function ResizeImages() { "
                    "var myimg,oldwidth;"
                    "var maxwidth=%f;" //缩放系数
                    "for(i=0;i <document.images.length;i++){"
                    "myimg = document.images[i];"
                    "if(myimg.width > maxwidth){"
                    "oldwidth = myimg.width;"
                    "myimg.width = maxwidth;"
                    "myimg.height = myimg.height * (maxwidth/oldwidth);"
                    "}"
                    "}"
                    "}\";"
                    "document.getElementsByTagName('head')[0].appendChild(script);",width];
    [webView stringByEvaluatingJavaScriptFromString:js];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    //调整其他相关view的frame
    
    CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
    CGRect frame = webView.frame;
    frame.size.height = height + 90 + 200;

    
    //代理回调用来刷新数据
    if(self.backWebViewHeightBlock)
    {
        self.backWebViewHeightBlock(self,frame.size.height);
    }
    

}






@end
